﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace PJ.Data
{
    public class ItemWCFProxyProvider<C, S, D, I> : EntityWCFProxyProvider<C, S, D, I>, IItemProvider<I>
        where I : class, IItemData
        where D : BaseItemData, I, new()
        where S : class, IItemProvider<D>
        where C : ItemProviderClient<S, D>, new()
    {
        public ItemWCFProxyProvider(IItemCache<I> cache) : base (cache)
        {
        }


        public I GetByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException();
            try
            {
                return Cache.Add(ClientBase.GetByName(name));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public I FindByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException();
            try
            {
                return Cache.Add(ClientBase.FindByName(name));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public I CachedGetByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException();
            try
            {
                return Cache.Add(ClientBase.CachedGetByName(name));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public I CachedFindByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException();
            try
            {
                I cached = Cache.Add(ClientBase.CachedFindByName(name));
                return cached;
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public IEnumerable<string> Search(string searchText)
        {
            if (string.IsNullOrEmpty(searchText))
                throw new ArgumentNullException();
            try
            {
                return Cache.AddSearch(searchText, ClientBase.Search(searchText));
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        public IEnumerable<string> CachedSearch(string searchText)
        {
            if (string.IsNullOrEmpty(searchText))
                throw new ArgumentNullException();
            try
            {
                IEnumerable<string> list = Cache.Search(searchText);
                if (list == null)
                    return Cache.AddSearch(searchText, ClientBase.CachedSearch(searchText));
                else
                    return list;
            }
            catch (Exception)
            {
                ClientBase = null;
                throw;
            }
        }
        #region Interface override
        IItemData IItemProvider.GetByName(string name)
        {
            return GetByName(name);
        }

        IItemData IItemProvider.CachedGetByName(string name)
        {
            return CachedGetByName(name);
        }

        IItemData IItemProvider.FindByName(string name)
        {
            return GetByName(name);
        }

        IItemData IItemProvider.CachedFindByName(string name)
        {
            return GetByName(name);
        }
        #endregion

        #region Protected
        
        new protected IItemCache<I> Cache
        {
            get { return (IItemCache<I>)base.Cache; }
        }

        #endregion

    }
}
